package book.test05;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MRDriver {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.set("fs.default", "hdfs://192.168.0.155:9000/");
        try {
            Job job = Job.getInstance(conf);

            job.setJarByClass(MRDriver.class);
            job.setMapperClass(MRMapper.class);
            job.setReducerClass(MRReducer.class);

            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);

            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);

            FileInputFormat.setInputPaths(job, "/MR/test05/input");
            Path path = new Path("/MR/test05/input");
            if (path.getFileSystem(conf).exists(path)) {
                path.getFileSystem(conf).delete(path);
            }
            FileOutputFormat.setOutputPath(job, path);
            System.exit(job.waitForCompletion(true) ? 0 : 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
